const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'monitor.js'
  },
  mode: 'development',
  context: process.cwd(),
  devtool: 'source-map',
  devServer: {
    setupMiddlewares: (middlewares, devServer) => {
      if (!devServer) {
        throw new Error("webpack-dev-server is not defined");
      }
      middlewares.unshift({
        name: "success",
        // 接口路径
        path: "/api/success",
        middleware: (req, res) => {
          // mock 数据模拟接口数据
          res.send({ name: "tom" })
        },
      })
      middlewares.unshift({
        name: "error",
        // 接口路径
        path: "/api/error",
        middleware: (req, res) => {
          // mock 数据模拟接口数据
          res.sendStatus(500)
        },
      })
      return middlewares;
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      inject: 'head'
    })
  ]
}